package com.hy.example.server.web.api;


import com.hy.example.common.api.AbstractApi;
import com.hy.example.common.api.result.ApiPageResult;
import com.hy.example.common.api.result.ApiResult;
import com.hy.example.common.defs.CommonDefs;
import com.hy.example.server.common.param.ExampleDemoParam;
import com.hy.example.server.common.param.ExampleDemoQueryParam;
import com.hy.example.server.common.param.ExampleDemoUpdateParam;
import com.hy.example.server.common.vo.ExampleDemoItemVO;
import com.hy.example.server.common.vo.ExampleDemoVO;
import com.hy.example.server.core.service.ExampleDemoService;
import com.hy.example.server.web.convert.ExampleDemoConvert;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "示例接口")
@Slf4j
@RestController
@RequestMapping(CommonDefs.RESTFUL_URI_CONTEXT + "/example")
public class ExampleDemoApi extends AbstractApi {

    @Autowired
    private ExampleDemoConvert exampleDemoConvert;
    @Autowired
    private ExampleDemoService exampleDemoService;

    @Operation(summary = "分页列表")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ApiResult<ApiPageResult<ExampleDemoItemVO>> page(@RequestBody @Validated ExampleDemoQueryParam queryParam, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return ApiResult.success(exampleDemoService.page(queryParam, pageNum, pageSize));
    }


    @Operation(summary = "查看")
    @GetMapping("/view/{id}")
    public ApiResult<ExampleDemoVO> view(@Parameter(description = "摄像仪区域ID") @PathVariable Long id) {
        return ApiResult.success(exampleDemoConvert.convertVO(exampleDemoService.detail(id)));
    }

    @Operation(summary = "新增")
    @PostMapping("/add")
    public ApiResult<Boolean> add(@Validated @RequestBody ExampleDemoParam param) {
        exampleDemoService.add(param);
        return ApiResult.success();
    }

    @Operation(summary = "编辑")
    @PostMapping("/update")
    public ApiResult<Boolean> update(@Validated @RequestBody ExampleDemoUpdateParam param) {
        exampleDemoService.update(param);
        return ApiResult.success();
    }

    @Operation(summary = "删除")
    @PostMapping("/delete/{id}")
    public ApiResult<Boolean> delete(@Parameter(description = "ID") @PathVariable Long id) {
        exampleDemoService.delete(id);
        return ApiResult.success();
    }

}
